﻿Public Class frmStatusLight

    Private Sub frmStatusLight_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Bind Form objects to Gradient Panel Properties
        pbOffColor.DataBindings.Add("BackColor", StatusLight1, "OffColor")
        pbOnColor.DataBindings.Add("BackColor", StatusLight1.ThresholdOn, "Color")
        pbT1Color.DataBindings.Add("BackColor", StatusLight1.Threshold1, "Color")
        pbT2Color.DataBindings.Add("BackColor", StatusLight1.Threshold2, "Color")

        nudOnValue.DataBindings.Add("Value", StatusLight1.ThresholdOn, "Value")
        nudT1Value.DataBindings.Add("Value", StatusLight1.Threshold1, "Value")
        nudT2Value.DataBindings.Add("Value", StatusLight1.Threshold2, "Value")

        trackValue.DataBindings.Add("Value", StatusLight1, "Value")
        lblValue.DataBindings.Add("Text", StatusLight1, "Value")

        'Add handlers for color selections
        AddHandler pbOffColor.Click, AddressOf ChooseColor
        AddHandler pbOnColor.Click, AddressOf ChooseColor
        AddHandler pbT1Color.Click, AddressOf ChooseColor
        AddHandler pbT2Color.Click, AddressOf ChooseColor


        'Add Handler for Form object property changes
        AddHandler pbOffColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbOnColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbT1Color.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbT2Color.BackColorChanged, AddressOf ControlValueChanged

        AddHandler nudT1Value.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudT2Value.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudOnValue.ValueChanged, AddressOf ControlValueChanged

        AddHandler trackValue.ValueChanged, AddressOf ControlValueChanged
    End Sub

    Private Sub ChooseColor(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim pb As PictureBox = CType(sender, PictureBox)
        With Me.ColorDialog1
            .Color = pb.BackColor
            Dim res As DialogResult = .ShowDialog()

            If res = Windows.Forms.DialogResult.OK Then
                pb.BackColor = .Color
            End If
        End With
    End Sub

    Private Sub ControlValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Ctl As Control = CType(sender, Control)
        If Ctl.DataBindings.Count > 0 Then
            Ctl.DataBindings(0).WriteValue()
        End If
    End Sub
End Class